<?php
set_time_limit(0);
header("Content-type:text/html;charset=utf-8");
include_once dirname(dirname(__FILE__)) . '/nn_logic/nl_common.func.php';
define('XIAOLE_MOIVE_IMPORT_SP', 'gdgd_xiaole');
$dc = nl_get_dc(array(
    'db_policy' => NL_DB_WRITE,
    'cache_policy' => NP_KV_CACHE_TYPE_MEMCACHE
));

$sql_encode = "select * from (select count(*) as count,nns_queue_name,GROUP_CONCAT(nns_id) as group_id from nns_file_encode where nns_sp_id='".XIAOLE_MOIVE_IMPORT_SP."' group by nns_queue_name) as temp where temp.count>1 limit 200";
echo $sql_encode."<br/>";
$result_encode = nl_query_by_db($sql_encode,$dc->db());
if(!is_array($result_encode) || empty($result_encode))
{
    die();
}
foreach ($result_encode as $encode_value)
{
    $arr_group_id = explode(',', $encode_value['group_id']);
    $sql_encode_again = "select * from nns_file_encode where nns_sp_id='".XIAOLE_MOIVE_IMPORT_SP."' and nns_id in('".implode("','", $arr_group_id)."') order by nns_create_time asc";
    echo $sql_encode_again;
    $result_encode_again = nl_query_by_db($sql_encode_again,$dc->db());
    if(!is_array($result_encode_again) || empty($result_encode_again))
    {
        continue;
    }
    $arr_del_id = null;
    var_dump($result_encode_again,$sql_encode_again);
    echo "<br>";
    foreach ($result_encode_again as $encode_again_value)
    {
        if(strlen($encode_again_value['nns_loadbalance_id']) >0)
        {
           $sql_task = "select * from nns_mgtvbk_c2_task where nns_org_id='".XIAOLE_MOIVE_IMPORT_SP."' and nns_ref_id='{$encode_again_value['nns_out_media_id']}' and nns_type='media'";
           $result_task = nl_query_by_db($sql_task, $dc->db());
           if(is_array($result_task) && !empty($result_task))
           {
               continue;
           }
           
        }
        else
        {
            $sql_op="select * from nns_mgtvbk_op_queue where nns_media_id='{$encode_again_value['nns_out_media_id']}'";
            $result_queue = nl_query_by_db($sql_op, $dc->db());
            if(is_array($result_queue) && !empty($result_queue))
            {
                continue;
//                 nl_execute_by_db("delete from nns_mgtvbk_op_queue where nns_id='{$encode_again_value['nns_op_id']}'", $dc->db());
            }
        }
       $arr_del_id[] = array('media'=>$encode_again_value['nns_out_media_id'],'queue'=>$encode_again_value['nns_id']);
    }
    if(!is_array($arr_del_id) || empty($arr_del_id))
    {
        
        continue;
    }
    var_dump($arr_del_id);
    echo "<br>";
    echo "---del---".count($arr_del_id)."<br>";
    echo "---exsit---".$encode_value['count']."<br>";
//     if(count($arr_del_id) >= $encode_value['count'])
//     {
//         array_shift($arr_del_id);
//     }
    echo "---last---".count($arr_del_id)."<br>";
    foreach ($arr_del_id as $del_value)
    {
        
            $sql_del = "delete from nns_vod_media where nns_id='{$del_value['media']}'";
            echo $sql_del."<br>";
            nl_execute_by_db($sql_del, $dc->db());
        
            $sql_del = "delete from nns_file_encode_log where nns_file_encode_id='{$del_value['queue']}'";

            echo $sql_del."<br>";
            nl_execute_by_db($sql_del, $dc->db());
            $sql_del = "delete from nns_file_encode where nns_id='{$del_value['queue']}'";
            echo $sql_del."<br>";
            nl_execute_by_db($sql_del, $dc->db());
    }
    
    echo "<hr/>";
}
